<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MELANGE.
 *
 * PHP-MELANGE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MELANGE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MELANGE; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-melange
 * @subpackage  cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

require_once("mvc/action/Action.php");

class SaveAction extends Action {

    private $log;

    public function __construct() {
        $this->log =& LoggerManager::getLogger("SaveAction");
    }

    public function execute(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        if($mapping->getPath() == "/item/module") {

            return $mapping->getInputForward();

        }

        $errors = new ActionMessages();

        $itemI18n =& $form->getItemI18n();
        $itemI18n->setModifiedDate(date(_Core::FORMAT_SQLDATETIME));

        $item =& $itemI18n->getMenuItem();
        $item->setIsSeparator($item->getIsSeparator());



        if($item->getParentId() == 0 || strlen($item->getParentId()) == 0)
            $item->setParentId(null);


        if($item->getIsSeparator() == "1") {
            $itemI18n->setName("-------------------------");
            $item->setLink(null);
            $item->setCategoryId("foo");
            $item->setCategoryId(null);
        } else if(is_numeric($form->getModule()) && intval($form->getModule()) > 0) {
            $item->setCategoryId($form->getModule());
            $item->setLink($form->getInternalLink());
        } else if(is_string($form->getModule()) && strlen($form->getModule()) > 0) {
            $item->setCategoryId("foo");
            $item->setCategoryId(null);
            $item->setLink($form->getModule() . (strlen($form->getInternalLink()) > 0 ? "/" . $form->getInternalLink() : ""));
        } else {
            $item->setCategoryId("foo");
            $item->setCategoryId(null);
        }

        $conn = Propel::getConnection();

        try {

            $conn->begin();

            // process thumbnail
            $formThumbnail =& $form->getFormThumbnail();

            if(!$form->getDeleteThumbnail() && $formThumbnail["size"] > 0) {

                $thumbnail = $item->getImageRelatedByThumbnailId();
                $thumbnail->saveForm($formThumbnail, $conn);
                $item->setThumbnailId($thumbnail->getImageId());

            } else if($form->getDeleteThumbnail()) {

                $thumbnail = $item->getImageRelatedByThumbnailId();
                require_once("cms/core/om/BinaryFilePeer.php");
                BinaryFilePeer::doDelete($thumbnail->getBinaryFile(), $conn);

            }

            // process image
            $formImage =& $form->getFormImage();

            if(!$form->getDeleteImage() && $formImage["size"] > 0) {

                $image = $item->getImageRelatedByImageId();
                $image->saveForm($formImage, $conn);
                $item->setImageId($image->getImageId());

            } else if($form->getDeleteImage()) {

                $image = $item->getImageRelatedByImageId();
                require_once("cms/core/om/BinaryFilePeer.php");
                BinaryFilePeer::doDelete($image->getBinaryFile(), $conn);

            }

            if($itemI18n->getMenuItemI18nId() > 0) {

                MenuItemI18nPeer::doUpdate($itemI18n, $conn);

                $item->setMenuItemId($itemI18n->getMenuItemId());
                MenuItemPeer::doUpdate($item, $conn);

            } else {

                // An initial default language item is added
                if(strlen($itemI18n->getMenuItemId()) == 0) {

                    $language = $request->getContext()->
                        getInitParameter(_Core::DEFAULT_LANGUAGE);

                    $language = $language != $item->getMenu()->getLanguageCode() ?
                        $item->getMenu()->getLanguageCode() : $language;

                    $itemI18n->setLanguageCode($language);

                    $item->setPosition($item->getMaxPosition() + 1);
                    $itemI18n->setMenuItemId(MenuItemPeer::doInsert($item, $conn));

                }
                $itemI18n->setInsertDate(date(_Core::FORMAT_SQLDATETIME));
                $itemI18n->setActive(_Core::AUTO_ACTIVATE);
                MenuItemI18nPeer::doInsert($itemI18n, $conn);

            }

            $conn->commit();

        } catch(PropelException $e) {
            $conn->rollback();
            $errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("error.item.save", true));
            $this->log->error($e->getMessage());
        }


        if(!$errors->isEmpty()) {
            $this->saveErrors($request, $errors);
            return $mapping->getInputForward();
        }

        $session  = $request->getSession();
        $listForm = $session->getAttribute("php-cms/menu/itemListForm");
        if(!is_null($listForm)) {
            $listForm->setPage(1);
            $session->setAttribute("php-cms/menu/itemListForm", $listForm);
        }


        // Handle cache
        if($request->getContext()->getInitParameter(_Core::CACHE)) {
            // Get the cache
            $cache = $request->getContext()->getCache();
            // Clear the menu cache
            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('menu'));
            // Clear the page cache
            $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('page'));
        }


        // Return to index
        return $mapping->findForward("success");

    }

}
?>